<template>
  <el-dialog
    title="添加角色"
    width="30%"
    :append-to-body="true"
    :visible.sync="showTag"
  >
    <el-form :model="form" size="mini" label-width="80px">
      <el-form-item label="名称">
        <el-input v-model="form.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="form.sort" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="sub">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: "RoleAddDialog",
  data() {
    return {
      info: {},
      form: {},
      showTag: false
    };
  },
  methods: {
    show(info) {
      this.info = info;
      this.setForm();
      this.showTag = true;
    },
    hide() {
      this.showTag = false;
    },
    setForm() {
      const { info } = this;
      ["name", "sort"].forEach(r => {
        info && info[r] && this.$set(this.form, r, info[r]);
      });
    },
    async sub() {
      if (this.info.id) {
        if (
          await this.$adminService.roleUpdate({
            id: this.info.id,
            ...this.form
          })
        ) {
          this.$message.success("修改成功!");
          this.hide();
          this.$emit("ok");
        }
        return;
      }
      if (await this.$adminService.roleAdd(this.form)) {
        this.$message.success("添加成功!");
        this.hide();
        this.$emit("ok");
      }
    }
  }
};
</script>

<style scoped></style>
